package the.workshop.milktea.interfaces.controller;

import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.wisteria.framework.util.DateUtils;
import org.wisteria.framework.web.APIResult;
import org.wisteria.framework.web.QueryResult;
import org.wisteria.framework.web.mvc.dto.SelectedDTO;
import the.broccoli.auth.UserContext;
import the.workshop.milktea.interfaces.req.CategoryAddReq;
import the.workshop.milktea.interfaces.req.CategoryModifyReq;
import the.workshop.milktea.interfaces.req.CategoryPageReq;
import the.workshop.milktea.interfaces.req.CategoryRemoveReq;
import the.workshop.milktea.interfaces.resp.CategoryGetResp;
import the.workshop.milktea.interfaces.resp.CategoryPageResp;
import the.workshop.milktea.service.CategoryService;
import the.workshop.milktea.service.dto.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 种类 前端控制器
 * </p>
 *
 * @author huamu
 * @since 2023-04-01
 */
@Api(tags = "Tea")
@RestController
@RequestMapping("/milktea/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @PostMapping("/add")
    public APIResult<Boolean> add(@RequestBody CategoryAddReq req) {
        CategoryAddDTO dto = new CategoryAddDTO();
        BeanUtils.copyProperties(req, dto);
        dto.setOperatorId(UserContext.userId());
        dto.setOperatorName(UserContext.userName());
        return APIResult.success(categoryService.add(dto));
    }

    @PostMapping("/modify")
    public APIResult<Boolean> modify(@RequestBody CategoryModifyReq req) {
        CategoryModifyDTO dto = new CategoryModifyDTO();
        BeanUtils.copyProperties(req, dto);
        dto.setOperatorId(UserContext.userId());
        dto.setOperatorName(UserContext.userName());
        return APIResult.success(categoryService.modify(dto));
    }

    @PostMapping("/remove")
    public APIResult<Boolean> remove(@RequestBody CategoryRemoveReq req) {
        categoryService.removeByIds(req.getIds(), UserContext.userId(), UserContext.userName());
        return APIResult.success();
    }

    @GetMapping("/page")
    public APIResult<QueryResult<CategoryPageResp>> page(CategoryPageReq req) {
        CategoryPageDTO dto  = new CategoryPageDTO();
        BeanUtils.copyProperties(req, dto);
        dto.setPageNum(req.getPageNum());
        dto.setPageSize(req.getPageSize());
        QueryResult queryResult = categoryService.pageList(dto, req.getPageNum(), req.getPageSize());
        return APIResult.success(queryResult);
    }

    @GetMapping("/{id}")
    public APIResult<CategoryGetResp> get(@PathVariable(value = "id") Integer id) {
        CategoryGetResp resp = null;
        CategoryGetDTO dto = categoryService.get(id);
        if (dto != null) {
            resp = new CategoryGetResp();
            BeanUtils.copyProperties(dto, resp);
        }
        return APIResult.success(resp);
    }

    @GetMapping("/selected/option")
    public APIResult<List<SelectedDTO>> getSelectedOption() {
        List<CategoryPageResultDTO> list = categoryService.getList();
        return APIResult.success(CollectionUtils.isEmpty(list) ? null : list.stream().map(this::convertOf).collect(Collectors.toList()));
    }

    private SelectedDTO convertOf(CategoryPageResultDTO source) {
        SelectedDTO target = new SelectedDTO();
        target.setDictCode(source.getCategoryId() + "");
        target.setDictName(source.getName());
        return target;
    }
}
